﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;
using System.Web.Security;
using Coslen.TahorCRM.Security;

namespace Coslen.TahorCRM.UI.www.Helpers
{
    public class SecurityPrincipalHelper
    {
        private static IPrincipal GetPrincipalFromUserName(string userName, bool isAuthenticated)
        {
            // Put the Princpal and Identit into Cache...
            TahorMembershipService service = new TahorMembershipService();
            IPrincipal principal = service.GetPrincipalForUserName(userName, isAuthenticated);

            return principal;
        }

        public static void SignOffPrincipal(object httpContext, IPrincipal principal)
        {
            HttpContext.Current.User = null;
            Thread.CurrentPrincipal = null;

            // Sign out
            FormsAuthentication.SignOut();
        }
        
        public static void SignOnPrincipal(HttpContextBase httpContext, string userName, bool createPersistentCookie)
        {
            // Put the Princpal and Identit into Cache...
            IPrincipal principal = GetPrincipalFromUserName(userName, true);
            HttpCacheHelper.SetCachePrincipal(principal, createPersistentCookie, httpContext);
            HttpContext.Current.User = principal;
            Thread.CurrentPrincipal = principal;

            // This is handled in the SetCachePrincipal
            //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        public static IPrincipal GetPrincipalFromCacheOrCookie(HttpContext httpContext)
        {
            bool foundPrincipal = false;
            IPrincipal user = httpContext.User;

            if (user is TahorPrincipal)
            {
                foundPrincipal = true;
            }

            if (foundPrincipal == false && user.Identity.IsAuthenticated)
            {
                // Put the Princpal into current Thread....
                string cacheKey = HttpCacheHelper.GetUserPrincipalKey(user.Identity.Name);
                IPrincipal principal = HttpContext.Current.Cache.Get(cacheKey) as TahorPrincipal;
                if (principal != null)
                {
                    user = principal;
                    foundPrincipal = true;
                }
            }

            if (foundPrincipal == false)
            {
                try
                {
                    HttpCookie authCookie = CookieHelper.GetCookie(CookieHelper.AuthCookieKey);

                    if (authCookie != null)
                    {
                        // Extract the roles from the cookie, and assign to our current principal, which is attached to the HttpContext. 
                        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

                        // Put the Princpal and Identit into Cache...
                        IPrincipal principal = GetPrincipalFromUserName(ticket.Name, true);

                        if (principal != null)
                        {
                            HttpCacheHelper.SetCachePrincipal(principal, ticket.IsPersistent, HttpContext.Current);
                            user = principal;
                            foundPrincipal = true;
                        }

                        // Delete the cookie
                        CookieHelper.RemoveCookie(CookieHelper.AuthCookieKey);
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

            if (foundPrincipal)
            {
                return user;
            }
            else
            {
                return null;
            }

        }
    }
}